package algorithm.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.InitialContext;
import javax.sql.DataSource;

public class DBUtil {
	private static final String DRIVER = "oracle.jdbc.driver.OracleDriver";
	private static final String URL = "jdbc:oracle:thin:@127.0.0.1:1521:xe";
	private static final String USER = "algorithm";
	private static final String PASSWORD = "algorithm";
	private static DataSource env;
	static{
		  try {
			  env = (DataSource) new InitialContext().lookup("java:comp/env/jdbc/Oracle");  
		  } catch (Exception e ) { 
		  	try{Class.forName(DRIVER);}catch(Exception e1){}
		  } 
	}
	
	public 	static Connection getConnection() throws SQLException{
		if(env ==null){
			return DriverManager.getConnection(URL, USER, PASSWORD);
		}else{
			return env.getConnection();
		}
	}
	public static void close(ResultSet rs){
		if(rs != null){
			try {
				
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	public static void close(Statement stmt){
		
		if(stmt != null){
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	public static void close(Connection conn){
		
		if(conn != null){
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	
	
}
